<script type="text/javascript" src="${ctx}/workflowStatic/js/javacode/codemirror.js"></script>
<script type="text/javascript" src="${ctx}/workflowStatic/js/javacode/InitMirror.js"></script>
<script>
    //初始化方法如果放到layui.use块中，则脚本无法回显
    var $ = layui.$;
    $(function(){
        init();
    });
    //初始化
    function init(){
        var scope = $("#scope").val();
        if(scope){
            var JsonObj = JSON.parse(scope);
            var type = JsonObj.type;
            var typeVal=JsonObj.value;
            if(type == 'script'){
                $("#type option[value='script']").attr("selected", true);
                $("#scriptEditDiv").show();
                $("#script").val(typeVal);
            }else{
                if(typeVal){
                    $("#type option[value='"+typeVal+"']").attr("selected", true);
                }
            }
        }
    }
</script>
<form class="layui-form" style="margin-top: 10px;">
    <table class="layui-table" style="width: 98%;margin:auto">
        <tr>
            <td style="width: 90px;text-align: right">人员选择范围:</td>
            <td>
                <select id="type" name="type" lay-verify="required" lay-filter="type">
                    <option value="all">全部</option>
                    <option value="department">部门</option>
                    <option value="script">脚本</option>
                </select>
            </td>
        </tr>
        <tr id="scriptEditDiv" style="display:none;">
            <td style="width: 90px;text-align: right">脚本:</td>
            <td>
                <span class="commonScript" style="cursor: pointer;">
                    <i class="layui-icon layui-icon-fonts-code"></i>常用脚本
                </span>
                <br/>脚本中要使用到其他字段参与运算， 请使用“[字段名]”方式引用。脚本需要返回一个组织ID<br />
                <textarea id="script" name="script" codemirror="true" class="layui-textarea"></textarea>
            </td>
        </tr>
    </table>
    <input type="hidden" id="setId" name="setId" value="${bpmNodeSet.setId}" />
    <input type="hidden" id="scope" name="scope" value='${bpmNodeSet.scope}' />
    <div class="bottom" style="position: absolute;bottom:15px;text-align: center;width:100%;">
        <button type="button" lay-submit class="layui-btn">保存</button>
        <button type="button" class="layui-btn layui-btn-danger cancel">取消</button>
    </div>
</form>

<script>
    layui.use(['form','layer','setter','commons'], function(){
        var form = layui.form
            ,$ = layui.$
            ,layer = layui.layer
            ,setter = layui.setter
            ,commons = layui.commons;

        form.on('select(type)', function(data){
            if(data.value=="script"){
                $("#scriptEditDiv").show();
            }else{
                $("#scriptEditDiv").hide();
            }
        });

        $(".commonScript").click(function(){
            var scriptWin;
            parent.layer.open({
                type: 2,
                area: ["800px","650px"],
                title: ['脚本'],
                content: serverPath.workflowPath + "/form/table/view/script?param=3",
                success: function(layero, index){
                    scriptWin = parent.window[layero.find('iframe')[0]['name']];
                },
                end: function(){
                    if(scriptWin.layui.scriptText){
                        InitMirror.editor.insertCode(scriptWin.layui.scriptText);
                    }
                }
            });
        });

        /**
         * 取消
         */
        $(".cancel").click(function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        });

        /**
         * 保存
         */
        //监听提交
        form.on('submit', function(data){
            InitMirror.save();
            var type = data.field.type;
            var scope ={};
            if(type == 'script'){
                scope.type = "script";
                scope.value = $("#script").val();
            }else{
                scope.type = "system";
                scope.value = data.field.type;
            }
            $.ajax({
                url: serverPath.workflowPath+setter.workflowLinkPath+"bpmNodeSet/saveScope",
                type: "POST",
                dataType:"json",
                data: {setId:data.field.setId,scope:JSON.stringify(scope)},
                success: function (data) {
                    if (commons._filterRecord(data)) {
                        window.parent.layer.alert(data.msg, function (index) {
                            parent.layer.close(index);
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        });
                    } else {
                        window.parent.layer.alert(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert("保存失败！" + XMLHttpRequest.status);
                },
            });
            return false;
        });
    });
</script>